# -*- coding:utf-8 -*-

import threading
from pynput import keyboard, mouse

from record.get_from_mouse_keybord.listener_keyboard import press_key, release_key
from record.get_from_mouse_keybord.listener_mouse import move_mouse, click_mouse,scroll_middle



class Cord:
    instance = None
    lock = threading.RLock()
    mouseListener: mouse.Listener = None
    keyboardListener: keyboard.Listener = None

    def __new__(cls, *args, **kwargs):
        if cls.instance:
            return cls.instance

        with cls.lock:
            cls.instance = object.__new__(cls)
            return cls.instance

    @classmethod
    def start_record(cls):
        cls.mouseListener = mouse.Listener(on_move=move_mouse, on_click=click_mouse, on_scroll=scroll_middle)
        cls.mouseListener.start()
        cls.keyboardListener = keyboard.Listener(on_press=press_key, on_release=release_key)
        cls.keyboardListener.start()

    @classmethod
    def pause_record(cls):
        cls.mouseListener.stop()
        cls.keyboardListener.stop()

    @classmethod
    def continue_record(cls):
        cls.mouseListener = mouse.Listener(on_move=move_mouse, on_click=click_mouse, on_scroll=scroll_middle)
        cls.mouseListener.start()
        cls.keyboardListener = keyboard.Listener(on_press=press_key, on_release=release_key)
        cls.keyboardListener.start()

    @classmethod
    def stop_record(cls):
        cls.mouseListener.stop()
        cls.keyboardListener.stop()


